﻿var win = null;
var callback = null;

(function ($) {
    var twoMenu = $(".container-left");
    var threeMenu = $("#subNav");
    var framesContainer = $("#iframe");
    var arrow = $(".arrow-0");

    function activeTab(key, name, url) {

        var pNum = 0; // 当前二级导航的父导航id

        twoMenu.find('li').each(function (i, v) {
            $(this).find('a').removeClass('active');
            if ($(this).attr('key') === key) {
                twoMenu.find('ul').hide();
                $(this).parent().show();
                $(this).find('a').addClass('active');
                pNum = $(this).parent().attr('data-id');
            }
        });
        arrow.css('top', (45 + 86 * pNum) + 'px');

        var isNull = true;
        threeMenu.find('li').each(function (index, v) {
            $(this).removeClass('active');
            if ($(this).attr('key') === key) {
                isNull = false;
                $(this).addClass('active');
                var iframeOpen;
                framesContainer.find('iframe').hide().each(function (i, item) {
                    if ($(item).attr("key").indexOf(key) !== -1) {
                        iframeOpen = $(item);
                    }
                    if ($(item).attr("key").indexOf(key + "_Next") !== -1) {
                        iframeOpen = $(item);
                    }
                });
                iframeOpen.show();
            }
        });
        if (isNull) {
            var li = '<li class="active" name="' + name + '" url="' + url + '" key="' + key + '">' + name + '<span class="delete"></span></li>';
            threeMenu.append(li);
            var frame = '<iframe frameborder="no" border="0" src="' + url + '" name="' + name + '" url="' + url + '" key="' + key + '" ></iframe>';
            var $iframe = $(frame).appendTo(framesContainer.find('iframe').hide().end());
            $('#iframe').append('<div id="loader-' + key + '" class="loading-iframe">加载中...</div>');
            $iframe.load(function () {
                $('#loader-' + key).remove();
            });
            $iframe.bind('error', function () {
                $('#loader-' + key).remove();
            });
        }
    }

    function removeFrame(key) {
        var $iframe = framesContainer.find('iframe[key^=' + key + ']');

        $iframe.each(function () {
            try {
                this.contentWindow.document.write('');//清空iframe的内容
                this.contentWindow.close();//避免iframe内存泄漏
            } catch (e) {

            }

            $(this).remove();
            if ($.browser.msie) {
                CollectGarbage();
            }
            // return false;

        });
    }

    function navTab(url) {
        var frame;
        framesContainer.find("iframe").each(function () {
            if ($(this).css('display') !== 'none') {
                frame = $(this);
            }
        });
        if (frame) {
            var keyNext = frame.attr('key') + "_Next";
            frame.hide();
            //            framesContainer.append('<iframe src="' + url + '" url="'+url+'" key="' + keyNext + '"></iframe>')
            $('<iframe frameborder="no" border="0" src="' + url + '" url="' + url + '" key="' + keyNext + '"></iframe>').insertAfter(frame);
        }
    }

    function navNewTab(url) {
        var frame;

        framesContainer.find("iframe").each(function () {
            if ($(this).css('display') !== 'none') {
                frame = $(this);
            }
        });
        if (frame) {
            var keyNext = frame.attr('key') + "_Next";
            frame.hide();
            //            framesContainer.append('<iframe src="' + url + '" url="'+url+'" key="' + keyNext + '"></iframe>')
            $('<iframe frameborder="no" border="0" src="' + url + '" url="' + url + '" key="' + keyNext + '"></iframe>').insertAfter(frame);
        }
    }

    function backTab(reload, data, message, delay) {
        var frame;
        var index;
        framesContainer.find("iframe").each(function (i, item) {
            if ($(this).css('display') !== 'none') {
                frame = $(this);
                index = i;
            }
        });
        var prev = frame.prev().show();
        frame.remove();
        if ($.browser.msie) {
            CollectGarbage();
        }
        if (typeof (CollectGarbage) != 'undefined') {
            CollectGarbage();
        }

        if (reload == "reloadTree") {
            window.frames[index - 1].reloadTree(data);
        }
        else if (reload) {
            window.frames[index - 1].reload(data);
        }
    }

    function bykey(key) {
        return "[key='" + key + "']";
    }

    function bykeyAll(key) {
        return "[key*='" + key + "']";
    }

    function winOpen(options, callbackFunc) {
        options.close = function () {
            win.destroy();
            win = null;
            $("#win").remove();
        };

        if (win) return;
        callback = callbackFunc;
        $("#win").remove();
        if (options.iframe) {
            $("body").append("<div id='win'></div>");
        }
        else {
            $("body").append("<div id='win'>" + options.content + "</div>");
        }
        win = $("#win").kendoWindow(options).data("kendoWindow");
        win.center()
        win.open();
    }

    function winClose() {
        if (!win) return;
        win.destroy();
        win.close();
        win = null;
        $("#win").remove();
    }

    function winSize(title, width, height) {
        if (!win) return;

        win.setOptions(
            {
                title: title,
                width: width,
                height: height
            });
        win.center();
    }

    window.navNewTab = navNewTab;
    window.backTab = backTab;
    window.navTab = navTab;
    window.activeTab = activeTab;
    window.removeFrame = removeFrame;
    window.winSize = winSize;
    window.winClose = winClose;
    window.bykey = bykey;
    window.bykeyAll = bykeyAll;
    window.winOpen = winOpen;
})(jQuery);

